﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace SolutionGenerator
{
    public partial class WizardLoadConfiguration : UserControlParent
    {
        public WizardLoadConfiguration(SolutionConfiguration configuration)
            : base(configuration)
        {
            InitializeComponent();
        }
        private void SolutionGeneratorLoadConfigurationBrowse_Click(object sender, EventArgs e)
        {
            if (SolutionGeneratorLoadConfiguration.ShowDialog() == DialogResult.OK)
                SolutionGeneratorLoadConfigurationPathLabel.Text = SolutionGeneratorLoadConfiguration.FileName;
        }
        public override bool IsPageValid
        {
            get
            {
                if (!string.IsNullOrEmpty(SolutionGeneratorLoadConfigurationPathLabel.Text))
                    if (!File.Exists(SolutionGeneratorLoadConfigurationPathLabel.Text))
                        return false;
                return true;
            }
        }
        public override string ValidationMessage
        {
            get
            {
                return TextMessage.WizardValidation_LoadConfiguration;
            }
        }
        private void MapToSolutionConfiguration(SolutionConfiguration toMap)
        {
            GetSolutionConfiguration.ConnectionString = toMap.ConnectionString;
            GetSolutionConfiguration.GetFolderInfo = toMap.GetFolderInfo;
            GetSolutionConfiguration.GetTableInfo = toMap.GetTableInfo;
        }
        public override void Save()
        {
            if (!string.IsNullOrEmpty(SolutionGeneratorLoadConfigurationPathLabel.Text))
                using (SerializeProcessing<SolutionConfiguration> serial =
                     new SerializeProcessing<SolutionConfiguration>())
                    MapToSolutionConfiguration(serial.Deserialize(ReadXmlConfiguration));
        }
        public override void LoadWizard()
        {
            if (File.Exists(CommonFunction.StandarConfigFile))
                SolutionGeneratorLoadConfigurationPathLabel.Text = CommonFunction.StandarConfigFile;
        }
        public string ReadXmlConfiguration
        {
            get
            {
                return File.ReadAllText(SolutionGeneratorLoadConfigurationPathLabel.Text);
            }
        }
        public override string WizardTitle
        {
            get
            {
                return TextMessage.WizardTitle_LoadConfiguration;
            }
        }
    }
}